package com.hnluchuan.bmjlearning.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hnluchuan.bmjlearning.common.ServicePackageType;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hnluchuan.bmjlearning.dao.ServicePackageDAO;
import com.hnluchuan.bmjlearning.model.ServicePackage;
import com.hnluchuan.bmjlearning.dto.ServicePackageDTO;
import com.hnluchuan.core.support.Page;
import com.hnluchuan.utils.common.BeanMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ServicePackageService extends BaseService {
	
	@Autowired
	private ServicePackageDAO servicePackageDAO;
	
	public List<ServicePackageDTO> find(ServicePackageDTO query, Page page) {
		List<ServicePackage> models = servicePackageDAO.find(query, page);
		List<ServicePackageDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<ServicePackageDTO> findAll() {
		return this.find(new ServicePackageDTO(), null);
	}
	
	public List<ServicePackageDTO> findAll(ServicePackageDTO query) {
		return this.find(query, null);
	}
	
	public List<ServicePackageDTO> find(Page page) {
		return this.find(new ServicePackageDTO(), page);
	}
	
	public List<ServicePackageDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new ServicePackageDTO(), page);
	}
	
	public Long create(ServicePackageDTO dto) {
		ServicePackage model = new ServicePackage();
		toModel(model, dto);
		return servicePackageDAO.create(model);
	}
	
	public ServicePackageDTO load(Long id) {
	    ServicePackage model = servicePackageDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(ServicePackageDTO dto) {
		ServicePackage model = servicePackageDAO.load(dto.getId());
		toModel(model, dto);
		servicePackageDAO.update(model);
    }

    @Transactional(rollbackFor = Exception.class)
	public int enableOrDisableByIds(Long[] ids, Integer status) {
	    List<Long> idsList = Arrays.asList(ids);
	    return servicePackageDAO.enableOrDisableByIds(idsList, status);
	}

	public List<ServicePackageDTO> findByAgentId(Long agentId){
		List<ServicePackage> models = servicePackageDAO.findByAgentId(agentId);
		return toDTOs(models);
	}

	//某代理商下的正式包
	public List<ServicePackageDTO> findOfficialByAgentId(Long agentId){
		List<ServicePackage> models = servicePackageDAO.findOfficialByAgentId(agentId);
		return toDTOs(models);
	}

	public List<ServicePackageDTO> findHistoryByAgentId(Long agentId){
		List<ServicePackage> models = servicePackageDAO.findHistoryByAgentId(agentId);
		return toDTOs(models);
	}

	public List<ServicePackageDTO> findHistoryByAgentIdAndName(Long agentId, String name){
		List<ServicePackage> models = servicePackageDAO.findHistoryByAgentId(agentId);
		return toDTOs(models);
	}

	//选中的包是否包含正式包
	public Boolean isIncludeOfficial(Map<Long, Integer> map){
		for(Map.Entry<Long, Integer> entry : map.entrySet()) {
			Integer count = entry.getValue();
			Long servicePackageId = entry.getKey();
			ServicePackageDTO load = this.load(servicePackageId);
			if (load.getType().equals(new Integer(ServicePackageType.OFFICIAL.getValue()))) {
				return true;
			}
		}
		return false;
	}

	public ServicePackageDTO findOne(ServicePackageDTO query) {
		ServicePackage model = servicePackageDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<ServicePackageDTO> toDTOs(List<ServicePackage> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<ServicePackageDTO>(0);
		}
		List<ServicePackageDTO> dtos = new ArrayList<ServicePackageDTO>(models.size());
		for (ServicePackage model : models) {
	        ServicePackageDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private ServicePackageDTO toDTO(ServicePackage model) {
		if (model == null) {
			return null;
		}
		ServicePackageDTO dto = BeanMapper.map(model, ServicePackageDTO.class);
		
		return dto;
	}
	
	private void toModel(ServicePackage model, ServicePackageDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<ServicePackage> toModels(List<ServicePackageDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<ServicePackage>(0);
		}
		List<ServicePackage> models = new ArrayList<ServicePackage>(dtos.size());
		for (ServicePackageDTO dto : dtos) {
	        ServicePackage model = new ServicePackage();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
